<?php
/*
 * Created on May 12, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Ncategory_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $category_parent_id  ;  
	/**
	 * @var unknown_type
	 */
	var $name_vi  ; 
	/**
	 * @var unknown_type
	 */
	var $name_en  ;  
	/**
	 * @var unknown_type
	 */
	var $description_vi;
	/**
	 * @var unknown_type
	 */
	var $description_en;
	/**
	 * @var unknown_type
	 */
	var $picture;
	/**
	 * @var unknown_type
	 */
	var $active;
	/*
	 * @access	public
	 */
	/*function Ncategory_model()
	{
		parent::Model();
	}*/

        function __construct()
	{
		parent::__construct();
	}
	
	function insertGroupUser($user_id, $group_id, $permission_id, $color_id, $active){
		$sql = "INSERT IGNORE INTO user_group (user_id, group_id, permission_id, color_id, active) VALUES ($user_id, $group_id, $permission_id, $color_id, $active)";
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function create($category){
		//if(!is_array($categories))
		//	$categories = array($categories);
		
		$sql = "INSERT INTO `gtb_ncategory` " .
				"(`id_parent_ncategory`, " .
				"`vi_name`, `en_name`, `en_description`, `vi_description`, " .
				"`link_rewrite`, `meta_title`, `meta_keywords`, " .
				"`meta_description`, `level_depth`, `position`,  `active`, `date_add`, `date_upd`) " .
				"VALUES";
		//for($i = 0; $i < count($categories); $i++){
		//	$category = $categories[$i];
			$sql .= "(" .
					intval($category['id_parent_ncategory']).", " .
					 $this->db->escape($category['vi_name']).", " .
					 $this->db->escape($category['en_name']).", " .
					 $this->db->escape($category['en_description']).", " .
					 $this->db->escape($category['vi_description']).", " .
					 $this->db->escape($category['link_rewrite']).", " .
					 $this->db->escape($category['meta_title']).", " .
					 $this->db->escape($category['meta_keywords']).", " .
					 $this->db->escape($category['meta_description'])."," .
					 intval($category['level_depth']).", " .
                                intval($category['position']).", " .
					 intval($category['active']).", " .
					 $this->db->escape($category['date_add']).", " .
					 $this->db->escape($category['date_upd'])."" .
					")";
				
		//}
		//$sql = substr($sql, 0, strlen($sql)-1);
		$this->db->query($sql);
		//return $this->db->affected_rows(); 
		$id = $this->db->insert_id();
		
		$sql = "UPDATE gtb_ncategory SET position = " . $id . " WHERE id_ncategory = " . $id;
		$this->db->query($sql);
		
		return $id;
	}
	
	function update($category){
		 $sql = "UPDATE gtb_ncategory 
				SET ".
				" id_parent_ncategory = " .  intval($category['id_parent_ncategory']) . ", " .
				" vi_name = " .  $this->db->escape($category['vi_name']) . "," .
				" en_name = " .  $this->db->escape($category['en_name']) . ", " .
				" vi_description = " .  $this->db->escape($category['vi_description']) . ", " .
				" en_description = " .  $this->db->escape($category['en_description']) . ", " .
				
				" link_rewrite = " .  $this->db->escape($category['link_rewrite']) . ", " .
				" meta_title = " .  $this->db->escape($category['meta_title']) . ", " .
				" meta_keywords = " .  $this->db->escape($category['meta_keywords']) . ", " .
				" meta_description = " .  $this->db->escape($category['meta_description']) . ", " .
				" level_depth = " .  $this->db->escape($category['level_depth']) . ", " .
				" active = " .  intval($category['active']) . ", " .
				" position = " .  intval($category['position']) . ", " .
				" date_upd = " .  $this->db->escape($category['date_upd']) .
				" WHERE id_ncategory = " .intval($category['id_ncategory']);
		$this->db->query($sql);
		
		return $this->db->affected_rows(); 
	}
		
	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_ncategory WHERE id_ncategory IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAllByCode($code_parent = '', $number_record = 0, $offset = 0){
		$result = null;
		
		if($number_record > 0){
			$sql = "SELECT id_pcategory, id_parent_pcategory, code_parent, code,vi_name,en_name, vi_description 
					FROM gtb_pcategory 
					WHERE (code_parent = '".trim($code_parent)."' )
					ORDER BY id_pcategory DESC
					LIMIT $offset, $number_record";
		}
		
		else $sql = "SELECT id_pcategory, id_parent_pcategory, code_parent, code,vi_name,en_name, vi_description 
				FROM gtb_pcategory ORDER BY id_pcategory DESC";
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result();
       	$query->free_result();
		return $result;
	}
	
	function getAll($number_record = 0, $offset = 0, $parent =  -1, $active = -1){
		if($number_record){
			$sql = "SELECT id_ncategory, id_parent_ncategory, link_rewrite, code, code_parent, vi_name, vi_name as cname, en_name, vi_description, active, position
					FROM gtb_ncategory " .
				    " WHERE (id_parent_ncategory=$parent OR -1 = $parent) AND (active = $active OR -1 = $active)  ORDER BY position DESC " .
				    " LIMIT $offset, $number_record";
		}
		else
			$sql = "SELECT id_ncategory, id_parent_ncategory, code, code_parent, vi_name, en_name, vi_description, position
				FROM gtb_ncategory WHERE (id_parent_ncategory=$parent OR -1 = $parent) AND (active = $active OR -1 = $active) ORDER BY position DESC";
		
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result();
                $query->free_result();
                //print_r($result);

		return $result;
	}
	
	function countAll($parent =  -1){
		$result = null;	
		$sql = "SELECT COUNT(*) as num_record 
				FROM gtb_ncategory 
				WHERE (id_parent_ncategory = '".intval($parent)."'  )";
		
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	$query->free_result();
		return $result[0]->num_record;
	}
	
	function getByParentCode($code_parent){
		$result = null;
		$sql = "SELECT *
				FROM gtb_pcategory WHERE code_parent = ".$this->db->escape($code_parent)." ORDER BY id_pcategory DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getMaxPosition(){
		
		$sql = "SELECT MAX(position) as position_max
				FROM gtb_ncategory 
			";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
       
		return 0;
	}
	
	function getById($category_id){
		
		$sql = "SELECT *
				FROM gtb_ncategory 
				WHERE id_ncategory = " . intval($category_id);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}
       
		return null;
	}
	
		
	function getByLinkRewrite($link_rewrite){
		
		$sql = "SELECT *
				FROM gtb_ncategory 
				WHERE link_rewrite = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	function getAllAdmin($parent, $offset, $limit ){
		
		$sql = "SELECT a.*,".
			 		" (SELECT MAX(t3.position) FROM gtb_ncategory t3) as max_order, 
					(SELECT MIN(t4.position) FROM gtb_ncategory t4) as min_order, 
					(SELECT count(nn.id_ncategory) FROM gtb_ncategory nn WHERE nn.id_parent_ncategory = a.id_ncategory) as subcat,
					(SELECT (t5.id_ncategory) FROM gtb_ncategory t5 WHERE t5.position < a.position ORDER BY t5.position DESC LIMIT 0,1) as id_next,
					(SELECT (t6.id_ncategory) FROM gtb_ncategory t6 WHERE t6.position > a.position ORDER BY t6.position ASC LIMIT 0,1) as id_preview " .
					" FROM gtb_ncategory a".
					" WHERE a.id_parent_ncategory = ". intval($parent) .
					" ORDER BY a.position DESC" .  
				 	" LIMIT " . intval($offset) . ", " . intval($limit);
		
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result();
                $query->free_result();
                //print_r($result);
		return $result;
	}
	
	function getCountAllAdmin($parent = 0){
		$result = null;
		
		$sql = "SELECT COUNT(*) as num_record " .
				" FROM gtb_ncategory ".
				" WHERE id_parent_ncategory = ". intval($parent) . ' OR 0 = ' . intval($parent);
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	$query->free_result();
		return $result[0]->num_record;
	}
	
//	
}

// EOF

